package com.tzxx.spider.controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.tzxx.spider.domain.ArticleLink;
import com.tzxx.spider.domain.Template;
import com.tzxx.spider.service.ArticleLinkService;
import com.tzxx.spider.service.TemplateService;
import com.tzxx.spider.spiderbots.base.ParseDetailFactory;
import com.tzxx.spider.spiderbots.base.ParseListFactory;
import com.tzxx.spider.vo.Result;

/**
 * @author Created by tzxx
 * @since : 2019/7/19.
 */
@RestController
public class SpiderController {

    @Resource
    private TemplateService templateService;
    @Resource
    private ArticleLinkService articleLinkService;

    @GetMapping("/run")
    public Result runByType() {
        Template template = templateService.getTemplateById(1);
        ParseListFactory.getSpider(template).parseLinkList(5);
        return Result.ok();
    }

    @GetMapping("/run/{type}")
    public Result runByType(@PathVariable String type) {
        if ("bim".equalsIgnoreCase(type)) {
            Template template = templateService.getTemplateById(1);
            ParseListFactory.getSpider(template).parseLinkList(5);
        }
        return Result.ok();
    }

    @GetMapping("/consumer")
    public Result consumer() {
        Template template = templateService.getTemplateById(1);
        ArticleLink articleLink = ArticleLink.builder().templateId(1).url("http://www.ccgp.gov.cn/cggg/zygg/zbgg/201907/t20190722_12507552.htm").build();
        ParseDetailFactory.getSpider(template).parseArticleDetail(5, articleLink);
        return Result.ok();
    }

}
